package com.ljw.community.mapper;


import com.ljw.community.entity.FollowedTopic;
import com.ljw.community.entity.Tag;
import com.ljw.community.entity.Topic;
import com.ljw.community.entity.TopicResp;
import com.ljw.community.service.SqlService;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TopicMapper {

    @SelectProvider(method = "loadTopicsProvider", type = SqlService.class)
    List<TopicResp> loadTopics(String tag);

    @Select(value = "select * from tag where del_flag='0' order by id desc")
    List<Tag> loadTags();

    @SelectProvider(method = "loadMyTopicsProvider", type = SqlService.class)
    List<TopicResp> loadMyTopics(String date, String loginName);

    @Update(value = "update topic set del_flag = '1' where id = #{id} ")
    int deleteTopicById(String id);

    @UpdateProvider(method = "updateTopicByIdProvider", type = SqlService.class)
    int updateTopic(Topic topic);

    @SelectProvider(method = "loadFollowedTopicsProvider", type = SqlService.class)
    List<TopicResp> loadFollowedTopics(String tag, String loginName);

    @Update(value = "update followed_topic set del_flag = '1' where topic_id = #{id} ")
    int unfollowTopicByTopicId(String id);

    @InsertProvider(method = "addTopicProvider", type = SqlService.class)
    int addTopic(Topic topic);

    @Select(value = "select * from topic where id = #{id} and del_flag = '0' ")
    Topic getTopicById(String id);

    @SelectProvider(method = "getTopicRespByIdProvider", type = SqlService.class)
    TopicResp getTopicRespById(String id);

    @Insert(value = "insert into followed_topic(id,topic_id,creator_id,create_time) " +
            "values(#{id},#{topicId},#{creatorId},#{createTime})")
    int addFollowTopic(FollowedTopic followedTopic);

    @Select(value = "select * from followed_topic where creator_id = #{creatorId} and del_flag = '0'")
    List<FollowedTopic> loadFollowedTopicsByCreatorId(String creatorId);

    //管理员页面获取话题列表方法
    @Select(value = "select t.*,u.login_name creator_login_name,tag.tag_name from topic t,user u,tag " +
            "where t.creator_id = u.id AND t.tag_id=tag.id and t.del_flag = '0' " +
            "and (t.title like '%${query}%' or tag.tag_name like '%${query}%' or u.login_name like '%${query}%')  " +
            "order by t.create_time desc limit ${start},${limit}")
    List<TopicResp> loadTopicList(int start, int limit, String query);

    @Select(value = "select count(1) from topic where tag_id = #{tagId} and del_flag='0'")
    int countByTagId(String tagId);
}
